package com.wendy.system.conf.controller;

import com.wendy.common.core.domain.TableDataInfo;
import com.wendy.common.log.annotation.Log;
import com.wendy.common.log.enums.BusinessType;
import com.wendy.common.web.controller.BaseController;
import com.wendy.custom.security.annotation.RequiresPermissions;
import com.wendy.custom.security.utils.SecurityUtils;
import com.wendy.excel.ruoyi.utils.ExcelUtil;
import com.wendy.system.conf.domain.bo.SysDictTypeBo;
import com.wendy.system.conf.domain.vo.SysDictTypeExcelVo;
import com.wendy.system.conf.domain.vo.SysDictTypeVo;
import com.wendy.system.conf.entity.SysDictType;
import com.wendy.system.conf.errorcode.DictypeError;
import com.wendy.system.conf.service.ISysDictTypeService;
import com.wendy.utils.spring.MapstructUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author wendy
 */
@RestController
@RequestMapping("/dict/type")
public class SysDictTypeController extends BaseController {
    @Autowired
    private ISysDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/page")
    public TableDataInfo page(SysDictTypeBo dictType) {
        TableDataInfo<SysDictTypeVo> list = dictTypeService.selectDictTypePage(dictType);
        return list;
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictTypeBo dictType) {
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        ExcelUtil<SysDictTypeExcelVo> util = new ExcelUtil<SysDictTypeExcelVo>(SysDictTypeExcelVo.class);
        List<SysDictTypeExcelVo> result = MapstructUtils.convert(list, SysDictTypeExcelVo.class);
        util.exportExcel(response, result, "字典类型");
    }

    /**
     * 查询字典类型详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictId}")
    public SysDictTypeVo getInfo(@PathVariable Long dictId) {
        return MapstructUtils.convert(dictTypeService.selectDictTypeById(dictId), SysDictTypeVo.class);
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public int add(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            DictypeError.ADD_ERROR.formatFire(dict.getDictName());
        }
        dict.setCreateBy(SecurityUtils.getUsername());
        return dictTypeService.insertDictType(dict);
    }

    /**
     * 修改字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public int edit(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            DictypeError.EDIT_ERROR.formatFire(dict.getDictName());
        }
        dict.setUpdateBy(SecurityUtils.getUsername());
        return dictTypeService.updateDictType(dict);
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public boolean remove(@PathVariable Long[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return true;
    }

    /**
     * 刷新字典缓存
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public boolean refreshCache() {
        dictTypeService.resetDictCache();
        return true;
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public List<SysDictTypeVo> optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        List<SysDictTypeVo> result = MapstructUtils.convert(dictTypes, SysDictTypeVo.class);
        return result;
    }
}
